﻿//-----------------------------------------------------------------------
// <copyright file="CreateOrEditUserForm.cs" company="OOHM PROCESSAMENTO DE DADOS LTDA.">
//     Microsoft Public License (MS-PL) This license governs use of the accompanying
//     software. If you use the software, you accept this license. If you do not
//     accept the license, do not use the software. 1. Definitions The terms "reproduce,"
//     "reproduction," "derivative works," and "distribution" have the same meaning
//     here as under U.S. copyright law. A "contribution" is the original software,
//     or any additions or changes to the software. A "contributor" is any person
//     that distributes its contribution under this license. "Licensed patents"
//     are a contributor's patent claims that read directly on its contribution.
//     2. Grant of Rights (A) Copyright Grant- Subject to the terms of this license,
//     including the license conditions and limitations in section 3, each contributor
//     grants you a non-exclusive, worldwide, royalty-free copyright license to
//     reproduce its contribution, prepare derivative works of its contribution,
//     and distribute its contribution or any derivative works that you create.
//     (B) Patent Grant- Subject to the terms of this license, including the license
//     conditions and limitations in section 3, each contributor grants you a non-exclusive,
//     worldwide, royalty-free license under its licensed patents to make, have
//     made, use, sell, offer for sale, import, and/or otherwise dispose of its
//     contribution in the software or derivative works of the contribution in
//     the software. 3. Conditions and Limitations (A) No Trademark License- This
//     license does not grant you rights to use any contributors' name, logo, or
//     trademarks. (B) If you bring a patent claim against any contributor over
//     patents that you claim are infringed by the software, your patent license
//     from such contributor to the software ends automatically. (C) If you distribute
//     any portion of the software, you must retain all copyright, patent, trademark,
//     and attribution notices that are present in the software. (D) If you distribute
//     any portion of the software in source code form, you may do so only under
//     this license by including a complete copy of this license with your distribution.
//     If you distribute any portion of the software in compiled or object code
//     form, you may only do so under a license that complies with this license.
//     (E) The software is licensed "as-is." You bear the risk of using it. The
//     contributors give no express warranties, guarantees or conditions. You may
//     have additional consumer rights under your local laws which this license
//     cannot change. To the extent permitted under your local laws, the contributors
//     exclude the implied warranties of merchantability, fitness for a particular
// </copyright>
//-----------------------------------------------------------------------
namespace AbstractSaaSTemplate.UI.Web.Models.User{    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using AbstractSaaSTemplate.Application.Localization;
    using AbstractSaaSTemplate.Domain.Models.Enumerators;

    /// <summary>
    /// Formats the exchange of information in the cycle
    /// Controller > View > POST > Controller
    /// for the 'CreateOrEdit' action
    /// </summary>
    public class CreateOrEditUserForm
    {
        /// <summary>
        /// Initializes a new instance of the CreateOrEditUserForm class.
        /// </summary>
        public CreateOrEditUserForm()
        {
            this.Roles = new List<AccessRoles>();
        }

        /// <summary>
        /// Initializes a new instance of the CreateOrEditUserForm class
        /// with parameters which will be immediately set to the properties.
        /// </summary>
        /// <param name="roles">The bitwise of access roles which are related to a particular user.</param>
        public CreateOrEditUserForm(AccessRoles roles)
        {
            this.Roles = GetListOfRolesFromRoleBitwise(roles);
        }

        /// <summary>
        /// Gets the list of access roles set for this user.
        /// </summary>
        public IEnumerable<AccessRoles> Roles { get; private set; }

        /// <summary>
        /// Gets or sets the first name.
        /// </summary>
        [DataType(DataType.Text)]
        [Required(ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "TheFieldXIsRequired")]
        [StringLength(255, MinimumLength = 2, ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "TheLengthOfFieldXIsWrong")]
        [Display(ResourceType = typeof(Expressions), Name = "FirstName")]
        public string FirstName { get; set; }

        /// <summary>
        /// Gets or sets the last name.
        /// </summary>
        [DataType(DataType.Text)]
        [Required(ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "TheFieldXIsRequired")]
        [StringLength(255, MinimumLength = 2, ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "TheLengthOfFieldXIsWrong")]
        [Display(ResourceType = typeof(Expressions), Name = "LastName")]
        public string LastName { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether the user is enabled or disabled.
        /// </summary>
        [Display(ResourceType = typeof(Expressions), Name = "IsDisabled")]
        public bool IsDisabled { get; set; }

        /// <summary>
        /// Gets or sets the e-mail address of the user.
        /// </summary>
        [DataType(DataType.EmailAddress)]
        [Required(ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "TheFieldXIsRequired")]
        [StringLength(255, MinimumLength = 2, ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "TheLengthOfFieldXIsWrong")]
        [Display(ResourceType = typeof(Expressions), Name = "EmailAddress")]
        public string EmailAddress { get; set; }

        /// <summary>
        /// Gets or sets the culture-name for the user.
        /// </summary>
        [Required(ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "TheFieldXIsRequired")]
        [StringLength(5, MinimumLength = 2, ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "TheLengthOfFieldXIsWrong")]
        [Display(ResourceType = typeof(Expressions), Name = "CultureName")]
        public string CultureName { get; set; }

        /// <summary>
        /// Gets or sets login of the credentials for the user.
        /// </summary>
        [DataType(DataType.Text)]
        [Required(ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "TheFieldXIsRequired")]
        [StringLength(255, MinimumLength = 2, ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "TheLengthOfFieldXIsWrong")]
        [Display(ResourceType = typeof(Expressions), Name = "Login")]
        public string Login { get; set; }

        /// <summary>
        /// Gets or sets the password of the creentials for the user.
        /// </summary>
        [DataType(DataType.Password)]
        [Required(ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "TheFieldXIsRequired")]
        [StringLength(255, MinimumLength = 2, ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "TheLengthOfFieldXIsWrong")]
        [Display(ResourceType = typeof(Expressions), Name = "Password")]
        public string Password { get; set; }

        /// <summary>
        /// Retrieves a list of acess roles from a roles bitwise.
        /// </summary>
        /// <param name="rolesBitwise">The roles bitwise.</param>
        /// <returns>Returns an enumerable of controlled roles.</returns>
        private static IEnumerable<AccessRoles> GetListOfRolesFromRoleBitwise(AccessRoles rolesBitwise)
        {
            var featureNamesList = Enum.GetNames(typeof(AccessRoles));
            var featuresList = new List<AccessRoles>();

            foreach (var featureName in featureNamesList)
            {
                var feature = AccessRoles.None;
                if (!Enum.TryParse(featureName, out feature))
                {
                    continue;
                }

                if (feature == AccessRoles.None)
                {
                    continue;
                }

                if ((feature & rolesBitwise) != feature)
                {
                    continue;
                }

                featuresList.Add(feature);
            }

            return featuresList;
        }
    }
}

